Standard Library (STL)
C++ Standard Library defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures. The Standard Library consist of the three key components **containers**, **iterators**, and **algorithms**. Unary Function Binary Function When you define your own binary functions to use with STL algorithms, you derive them from these classes in order to automatically get all the typedefs. struct SomeFancyUnaryFunction: public std::unary_function { Result_t operator ()(Arg_t const &) { // ... } }; Now you don't need to manually provide the typedefs for argument_type, result_type etc. These structs, just like the iterator struct are there just for our convenience, in order to reuse the typedefs needed for algorithms Strings Comparisons Strings can be compared to strings of their own type and to arrays of characters with the same character type. template, chass A = allocator > class basic_string { int compare(const basic_string& s) const; // combined > and int compare(const Ch* p) const; } Thus, s.compare(s2) returns 0 if the strings have the same value, a negative number if s is lexicographically before s2, and a positive number otherwise. The ususal comparison operators , !=, >, <, >=, and <= are provided for basic_strings: template bool operator (const basic_string&, const basic_string&); template bool operator (const Ch*, const basic_string&); template bool operator (const basic_string&, const Ch*); // similar declarations for !=, >, <, >=, and <= Comparison operators are nonmember functions so that conversions can be applied in the same way to both operands. The versions taking C-style strings are provided to optimize comparisons agains string literals. Insert and Concatenation Of the operations that modify the value of a string, is one of the most common is appending to it - that is, adding characters to the end. Insertion at other points of a string is rarer: basic_string& operator+=(const basic_string& s); basic_string& operator+=(const Ch* p); basic_string& operator+=(const Ch c); // Add characters after (*this)- 1: // Insert characters before (*this)pos // Insert characters before iterator p Concatemation - constructing a string out of two strings by placing one after the other - is provided by the + operator: template basic_string operator+(const basic_string&, const basic_string&); Find There is a bewildering variety of functions for finding substrings: * Find subsequence, e.g. search() * Find subsequence searching backwards from the end, e.g. find_end() * Find character, e.g. find_first_of() * Find character from argument searching backwards from the end * Find character not in argument * Find character not in argument searching backwards from the end These are all **const** members. They exist to locate a substring for some use, but they do not change the value of the string to which they are applied. Replace Once a position in a string is identified, the vlaue of indvidual character positions can be changed using subscripting or whole substrings can be replaced with new characters: * Replace [ (*this)i, (*this)+ n [ with other characters * Remove characters from string ("replace with nothing") Note that the number fo new characters need not be the same as the number of characters previously in the string. The size of the string is changed to accomodate the new substring. In particular, erase simply removes a substring and adjusts its size accordingly. Substrings The substr funciton is simply a way of reading a part of a string. On the other hand, replace lets you write to a substring. Both rely on the low-level position plus number of characters notation. However, find lets us find bustrings by value. Together, they allow us to define a substring that can be used for both reading and writing. I/O Operations One of the main uses of strings is as the target of input and as the source of output. The << operator writes a string to an ostream. The >> operator reads a whitespace-terminated word to its string, expanding the string as needed to hold the word. Initial whitespace is skipped, and the terminating whitespace character is not entered into the string. The getline function reads a line terminated by eol to its string, expanding string as needed to hold the line. If no eol argument is provided, a newline \n is used as the delimiter. The line terminator is removed from the stream but not entered into the string. The getchar returns the next character from the standard input (stdin). It is equivalent to getc(stdin). The putchar writes character to the current position in the standard output (stdout) and advances the internal file position indicator to the next position. It is equivalent to putc(character, stdout). Conversion From number to string: #include ostringstream ss; ss << 123.0; int number = ss.str(); From string to number: int number1 = atoi("111test"); // 111 long number2 = atol("222test"); // 222 double number3 = atof("333.3test"); // 333.3 From lowercase to uppercase: int i = 0; char str[] = "Test String."; char c; while (stri) { c = stri; putchar (toupper©); i++; } References * Stack Overflow: STL vs. Standard Library Category:Standard Library